{extend name="common/base"}
{block name="title"}首页{/block}

{block name="content"}
        <!-- 内容区域 -->
        <div class="tpl-content-wrapper">
            <div class="row-content am-cf">
                <div class="row">
                    <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
                        <div class="widget am-cf">
                            <div class="widget-head am-cf">
                                <div class="widget-title am-fl">新增商品</div>
                                <div class="widget-function am-fr">
                                    <a href="javascript:;" class="am-icon-cog"></a>
                                </div>
                            </div>
                            <div class="widget-body am-fr"> 

                                <form class="am-form tpl-form-border-form tpl-form-border-br" action="{:Url('/admin/goods/insertGoods')}" enctype="multipart/form-data" method="post">
                                    <div class="am-form-group">
                                        <label for="user-name" class="am-u-sm-3 am-form-label">商品名称 <span class="tpl-form-line-small-title"> Name</span></label>
                                        <div class="am-u-sm-9">
                                            <input type="text" class="tpl-form-input" id="user-name" placeholder="请输入商品名称" name="name">
                                        </div>
                                    </div>
                                    <div class="am-form-group">
                                        <label class="am-u-sm-3 am-form-label">商品分类 <span class="tpl-form-line-small-title">Type</span></label>
                                        <div class="am-u-sm-9">
                                            <select name="fenlei_id" id="sel1" style="width:30%;">
                                                {volist name="list" id="vo"}
                                                <option value="{$vo.id}" style="color:#888;">{$vo.name}</option>
                                                {/volist}
                                            </select>
                                        </div>
                                    </div>
                                    <div class="am-form-group">
                                        <label class="am-u-sm-3 am-form-label">二级分类 <span class="tpl-form-line-small-title">SonType</span></label>
                                        <div class="am-u-sm-9">
                                            <select name="fenlei_son_id" id="sel2" style="width:30%;">
                                                {empty name="son_list"}
                                                <option value="" style="color:#888;">无</option>
                                                {else /}
                                                {volist name="son_list" id="vo"}
                                                <option value="{$vo.id}" style="color:#888;">{$vo.name}</option>
                                                {/volist}
                                                {/empty}
                                            </select>
                                        </div>
                                    </div>
                                    <div class="am-form-group">
                                        <label for="user-weibo" class="am-u-sm-3 am-form-label">商品图片 <span class="tpl-form-line-small-title">Images</span></label>
                                        <div class="am-u-sm-9">
                                            <div class="am-form-group am-form-file">
                                                <div class="tpl-form-file-img">
                                                    <img src="assets/img/a5.png" alt="">
                                                </div>
                                                <button type="button" class="am-btn am-btn-danger am-btn-sm">
                                                <i class="am-icon-cloud-upload"></i> 添加商品图片</button>
                                                <input id="doc-form-file" type="file" name="src" onchange="upimg(this)"><br>
                                                <!-- <img src="" alt="" id="img-view-box" width="150" style="margin-top:10px;"> -->
                                                <!-- <input type="hidden" id="img-value-box" name="src">
                                                <input type="hidden" id="thumb-img-value-box" name="thumb_src"> -->
                                                <div class="imgBox"></div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="am-form-group">
                                        <label for="user-intro" class="am-u-sm-3 am-form-label">商品简介 <span class="tpl-form-line-small-title">About</span></label>
                                        <div class="am-u-sm-9">
                                            <textarea class="" rows="10" id="user-intro" placeholder="请输入商品简介" name="abstract"></textarea>
                                        </div>
                                    </div>
                                    <div class="am-form-group">
                                        <div class="am-u-sm-9 am-u-sm-push-3">
                                            <button type="submit" class="am-btn am-btn-primary tpl-btn-bg-color-success ">确认提交</button>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>

            </div>
        </div>

<script>
function upimg(obj)
{
    if( obj.value == "" ) {
        return;
    }
    var formdata = new FormData(); 
    //<input type="file" name="img" />
    formdata.append("img" , $(obj)[0].files[0]);//获取文件法二
    $.ajax({
        type : 'post',
        url : "{:Url('admin/goods/upimg')}",
        data : formdata,
        cache : false,
        processData : false, // 不处理发送的数据，因为data值是Formdata对象，不需要对数据做处理
        contentType : false, // 不设置Content-type请求头
        success : function(ret){
            console.log(ret);
            var html = '<span><img src="'+ret.img+'" style="width:200px;height:120px;margin-top:10px;">';
                html += '<input type="hidden" value="'+ret.img+'" name="src[]" class="img-src-value">';
                html += '<input type="hidden" value="'+ret.img_thumb+'" name="thumb_src[]" class="img-thumb_src-value">' ;
                html += '<span onclick="delBox(this)" class="am-close am-close-alt am-close-spin" style="position:relative;left:-10px;top:-50px;z-index:100; cursor:pointer;">&times;</span></span>';
            $(".imgBox").append(html);
            },
        error : function(){ }
    });
}
function delBox(obj){
    $(obj).parent('span').remove();
}
 
function getSon(id){
    $.ajax({
        url:"{:Url('admin/Goods/get_sel')}",
        type:"get",
        data:{id:id},
        dataType:"json",//数据通信的类型
        success:function(data){
            console.log(data);
            var length = data.length;
            $("#sel2").empty();
            if(length==0){
                $("#sel2").append('<option value="" style="color:#888;">无</option>');
            }else{
                for(var i=0;i<length;i++){ 
                    $("#sel2").append('<option value="'+data[i].id+'" style="color:#888;">'+data[i].name+'</option>');
                }
            }
        }
    });
}
$("select#sel1").change(function(){
    getSon($(this).val());
})
</script>
{/block}